package com.yequan.sentinel;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yequan
 * @date 2021/12/30 11:42
 **/
public class SentinelDemo {

    public static void main(String[] args) {
        intiFlowRules();
        while (true) {
            testSentinel();
        }
    }

    private static void testSentinel() {
        try {
            Entry entry = SphU.entry("testSentinel");
            System.out.println("Test Sentinel " + System.currentTimeMillis());
        } catch (BlockException e) {
//            log.error("BlockException: ", e);
//            System.out.println("BlockException: "+ e.getMessage());
        }


    }

    private static void intiFlowRules() {
        List<FlowRule> rules = new ArrayList<>();
        FlowRule rule = new FlowRule();
        rule.setResource("testSentinel");
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        rule.setCount(1);
        rules.add(rule);
        FlowRuleManager.loadRules(rules);
    }

}
